package ph.loong.deliveryman.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toolbar;

import deliveryman.loong.ph.deliveryman.R;

/**
 * Author jc.lu.
 * Dates 2018/2/22.
 * Email Tbsupland@163.com.
 * desc
 */

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class BackToolBar extends Toolbar implements View.OnClickListener {
    private final Context mContext;
    private final String mTitleStr;
    private ImageView mImgBack;
    private TextView mTitleText;

    public BackToolBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        LayoutInflater.from(context).inflate(R.layout.tool_bar_view, this, true);
        String stringHeight = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "layout_height");
        try {
            int default_height = Integer.valueOf(stringHeight).intValue();
            if (default_height == ViewGroup.LayoutParams.WRAP_CONTENT) {
            }
        } catch (NumberFormatException e) {
        }

        //get attrs
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.backtoolbar);
        mTitleStr = typedArray.getString(R.styleable.backtoolbar_title);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        findView();
        initView();
    }

    private void initView() {
        mImgBack.setOnClickListener(this);
        setTitleText(mTitleStr);
    }

    private void setTitleText(String title) {
        if (!TextUtils.isEmpty(title)) {
            mTitleText.setText(title);
        }
    }

    private void findView() {
        mImgBack = findViewById(R.id.image_back);
        mTitleText = findViewById(R.id.title_text);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.image_back:
                if (mBackOnClickListener != null) {
                    mBackOnClickListener.onBackOnclick();
                }
                break;
        }
    }

    /**
     * 监听返回图标
     */
    public interface onBackOnClickListener {
        void onBackOnclick();
    }

    private onBackOnClickListener mBackOnClickListener;

    public void setBackOnClickListener(onBackOnClickListener listener) {
        this.mBackOnClickListener = listener;
    }
}
